<?php
namespace Quality\Start\App\Action;

use Symfony\Component\HttpFoundation\Request;
use InvalidArgumentException;
use Symfony\Component\Config\Definition\Exception\Exception;

class Login extends Action
{
    /**
     * @param \Symfony\Component\HttpFoundation\Request $request
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function login(Request $request)
    {
        $this->setAttributes($request);
        $login = $request->get('username');
        $pass = $request->get('password');
        if (empty($login) || empty($pass)) {
            throw new InvalidArgumentException('Usuário e senha devem ser preenchidos');
        }

        if ($this->userControl->login($login, $pass)) {
            return $this->redirect('universe/');
        }

        $this->params['erroLogin'] = 'Login ou senha inexistentes';
        return $this->render('index.html');
    }

    /**
     * @param \Symfony\Component\HttpFoundation\Request $request
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function loginForm(Request $request)
    {
        $this->setAttributes($request);

        return $this->render('login.html');
    }

    /**
     * @param \Symfony\Component\HttpFoundation\Request $request
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
     */
    public function logout(Request $request)
    {
        $this->setAttributes($request);
        $this->userControl->logout();
        return $this->redirect('/workspace/startproject/');
    }
}
